package com.zj.blockchain.controller;

import com.zj.blockchain.model.Block;
import com.zj.blockchain.model.Transaction;
import com.zj.blockchain.service.BlockService;
import com.zj.blockchain.service.PowService;
import com.zj.blockchain.util.BlockCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @className: BlockController
 * @description: TODO 类描述
 * @author: allnas
 * @date: 2020/2/29
 **/
@Api(tags = {"Block相关接口"})
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BlockController {

    private final BlockCache blockCache;

    private final BlockService blockService;

    private final PowService powService;

    /**
     * 查看当前节点区块链数据
     *
     * @return
     */
    @ApiOperation("查看当前节点区块链数据")
    @GetMapping("scan")
    public List<Block> scan() {
        return blockCache.getBlockChain();
    }

    /**
     * 查看当前节点区块链数据
     *
     * @return
     */
    @ApiOperation("查看当前节点区块链数据")
    @GetMapping("data")
    public List<Transaction> data() {
        return blockCache.getPackedTransactions();
    }

    /**
     * 创建创世区块
     *
     * @return
     */
    @ApiOperation("创建创世区块")
    @GetMapping("create")
    public List<Block> createFirstBlock() {
        blockService.createGenesisBlock();
        return scan();
    }

    /**
     * 挖矿生成新的区块
     *
     * @return
     */
    @ApiOperation("挖矿生成新的区块")
    @GetMapping("mine")
    public List<Block> createNewBlock() {
        powService.mine();
        return scan();
    }
}
